package com.example.app2.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.app2.pojo.Login_Info;
import com.example.app2.pojo.Login_Info;

import java.util.ArrayList;
import java.util.List;

public class LoginHelper extends SQLiteOpenHelper {
    static String DB_NAME="login.db";
    static int DB_VERSION=1;
    private static LoginHelper mHelper=null;

    private SQLiteDatabase mRDB=null;
    private SQLiteDatabase mWDB=null;
    private static String TABLE_NAME="login_info";

    private LoginHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    public  static LoginHelper getInstance(Context context)
    {
        if(mHelper==null)
        {
            mHelper=new LoginHelper(context);
        }
        return mHelper;
    }
    //打开读连接
    public SQLiteDatabase openReadLink()
    {
        if(mRDB==null||!mRDB.isOpen())
        {
            mRDB=mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    //打开写连接
    public SQLiteDatabase openWriteLink()
    {
        if(mWDB==null||!mWDB.isOpen())
        {
            mWDB=mHelper.getReadableDatabase();
        }
        return mWDB;
    }
    //关闭数据库连接
    public void closeLink()
    {
        if(mRDB!=null&&mRDB.isOpen())
        {
            mRDB.close();
            mRDB=null;
        }
        if(mWDB!=null&&mWDB.isOpen())
        {
            mWDB.close();
            mWDB=null;
        }
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
//        String sql_delete="DROP TABLE IF EXISTS "+TABLE_NAME;
//        db.execSQL(sql_delete);


        String create_sql="CREATE TABLE IF NOT EXISTS  login_info(id INTEGER PRIMARY KEY  AUTOINCREMENT NOT NULL," +
                "phone VARCHAR NOT NULL,password VARCHAR NOT NULL,remember INTEGER NOT NULL);";
        db.execSQL(create_sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
    public long insert(Login_Info info)
    {
        ContentValues values=new ContentValues();
        values.put("phone",info.phone);
        values.put("password",info.password);
        values.put("remember",info.remember);
        //第二个参数是为了避免插入键和值都是空的段，就是假如第三个参数为空的情况，为null则表示不插入，一般情况写null即可。
        try{
            mWDB.beginTransaction();
            mWDB.insert(TABLE_NAME,null,values);
            mWDB.setTransactionSuccessful();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            mWDB.endTransaction();
        }
        return 1;
    }
    public long deleteByPhone(String phone)
    {
        return mWDB.delete(TABLE_NAME,"phone=?",new String[]{phone});
    }

    public Login_Info queryByPhone(String phone)
    {
        String sql="select * from "+TABLE_NAME+" where phone=? and remember=1;";
        Cursor cursor = mRDB.rawQuery(sql, new String[]{phone});
        if(cursor.moveToNext())
        {
            Login_Info info =new Login_Info();
            info.phone=cursor.getString(1);
            info.password=cursor.getString(2);
            info.remember=cursor.getInt(3)==1?true:false;
            return info;
        }
        return null;
    }
    public Login_Info queryLastChecked()
    {
        String sql="select * from "+TABLE_NAME+" where remember=1 order by id DESC limit 1;";
        Cursor cursor = mRDB.rawQuery(sql,null);
        if(cursor.moveToNext())
        {
            Login_Info info =new Login_Info();
            info.phone=cursor.getString(1);
            info.password=cursor.getString(2);
            info.remember=cursor.getInt(3)==1?true:false;
            return info;
        }
        return null;
    }
    public void WStartTrans()
    {
        mWDB.beginTransaction();
    }
    public void WTransSuccessful()
    {
        mWDB.setTransactionSuccessful();
    }
    public void WEndTrans()
    {
        mWDB.endTransaction();
    }

}
